<?php
$ids = array();
foreach ($this->parents as $folder):
    $ids[] = $folder->id;
    $this->breadcrumbs()->addPage($folder->title, '#!', 'folder', array('id' => $folder->id));
endforeach;
echo $this->breadcrumbs()->addPage("{$this->folder->title}")->render(false)
?>
<table style="width: 100%">
    <tbody class="lista">
        <?php
        $doc = false;
        $total = 0;
        foreach ($this->childrens as $pasta) : if ($pasta->getType() !== 'cmis:folder'): $doc = true;
                ?>
                <tr>
                    <td <?php if ($pasta->id == $this->document)
            echo 'class="search_ok"'; $total++; ?>>
                        <div class="mark_document">
                            <input type="checkbox" name="arquivos[]" value="<?php echo $pasta->id ?>" />
                        </div>
                        <div class="pasta">
                            <h1><?php echo $pasta->getTitle() ?></h1>
                            <p>Criado em: <?php echo $pasta->published->format("d-m-Y H:i:s"); ?> Por: <?php echo $pasta->getAuthor(); ?></p>
                            <?php if ($pasta->updated->date == $pasta->published->date) : ?>
                                arquivo não atualizado
                            <?php else: ?>
                                Atualizado em: <?php echo $pasta->updated->format("d-m-Y H:i:s"); ?> para
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php
            endif;
        endforeach;
        if (!$doc)
            echo 'sem arquivos para essa pasta';
        else {
            $html = '<div class="mark_all"><input type="checkbox" name="all" id="all" value="" />
            <label for="all">Marcar todos</label></div><div class="total">Total de itens: <strong>' . $total . '</strong></div>';
            echo $this->pagenav()->openList();
            echo $this->pagenav()->customHelperLinks($html);
            echo $this->pagenav()->closeList();
        }
        ?>
    </tbody>
</table>
<?php if ($doc): ?>

    <div id="article" style="margin-top: 30px; opacity: 0.4">
        <form action="/ajax/update-arquivos" method="post" id="atu_arq" ajax="✓">
            <fieldset style="margin: 0">
                <legend>Atualizar Documentos</legend>

            </fieldset>
            <dl class="form">
                <dt><label for="tipo_documento" >Tipo de Documento:</label></dt>
                <dd>
                    <select name="tipo_documento" id="tipo_documento" disabled="disabled">
                        <?php foreach ($this->tiposdocumentos as $documento): ?>
                            <option value="<?php echo $documento->id; ?>"><?php echo $documento->nome; ?></option>
                        <?php endforeach; ?>
                    </select>
                </dd>
                <ul class="buttons">
                    <div id="hiddens">
                    </div>
                    <li style="margin-top: 20px;">
                        <button id="atu" disabled="disabled" type="submit">Atualizar</button>
                    </li>
                </ul>
        </form>
    </div>
<?php endif; ?>
<script type="text/javascript">
    // fechar modal de carregamento
    $(function(){
        $("#dialog").dialog( "close" );
    });
    // verificar click do botão de submit
    $(function(){
        $(".buttons li button#atu").click(function(e){
            e.preventDefault();
            var ok = confirm("Deseja realmente atualizar esses arquivos?");
            if (ok) {
                $("form#atu_arq").submit();
            }
        })
    });
    // logica do botão de ckeckbox
    $(function(){
        function atualizar(ok) {
            var o = "1.0"; var d = false;
            if (!ok) {
                o = "0.3";
                d = true;
            }
            $("#article").css({"opacity":o});
            $("#tipo_documento, #atu").attr("disabled",d);
        }

        $(".mark_all input").live("change",function(){
            var mar_all = $(this).is(':checked') ? true : false;
            $(".form ul.buttons #hiddens").html("");
            $(".mark_document input").each( function() {
                if (mar_all)
                    $(".form ul.buttons #hiddens").append('<input type="hidden" name="ids_arquivos[]" value="'+$(this).val()+'" />');
                $(this).attr("checked", mar_all);
            });
            if (mar_all)
                atualizar(true);
            else
                atualizar(false);
        });

        $(".mark_document input").live("change",function(){
            if ($(".mark_all input").is(':checked')) {
                $(this).attr("checked", false);
                $(".mark_all input").attr("checked", false);
            }
            $(".form ul.buttons #hiddens").html("");
            $(".mark_document input").each( function() {
                if($(this).is(':checked'))
                    $(".form ul.buttons #hiddens").append('<input type="hidden" name="ids_arquivos[]" value="'+$(this).val()+'" />');
            });
            var ok = false;
            $(".mark_document input").each( function() {
                if($(this).is(':checked')) {
                    ok = true;
                    return false;
                }
            });
            atualizar(ok);
        });
    });
    // lógica para abrir menu na pasta correta
    $(function(){
        var array = [];
        $("#breadcrumbs a").each(function(){
            array.push($(this).attr('href').split("id/")[1])
        });
        var tam = array.length;
        var i = 0;
        if ($("a[rel='ajax/getfolders/"+array[i]+"/']").parent('li').attr('class') == "directory collapsed") {
            timerval = setInterval(function(){
                var folder = $("a[rel='ajax/getfolders/"+array[i]+"/']");
                folder.attr("param", "");
                var abrir = folder.triggerHandler('click');
                if (abrir != undefined) {
                    if (i < tam) i++
                    else clearInterval(timerval);
                }
            }, 500);
        }
    });
</script>